﻿using System.Web.Mvc;
using System.Web.Routing;

namespace TestBat.Frontend.Helpers
{
    public static class ImageLinkHelper
    {
        public static string ImageLink(this HtmlHelper html, string action, string controller, object routeValues, string imageURL, string text)
        {
            // Create an instance of UrlHelper
            var url = new UrlHelper(html.ViewContext.RequestContext);
            // Create image tag builder
            var imageBuilder = new TagBuilder("img");
            // Add image attributes
            var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
            imageBuilder.MergeAttribute("src", urlHelper.Content(imageURL));
            imageBuilder.MergeAttribute("alt", text);
            // Create link tag builder
            var linkBuilder = new TagBuilder("a");
            // Add attributes
            linkBuilder.MergeAttribute("class", "imageLink");
            linkBuilder.MergeAttribute("href", url.Action(action, controller, new RouteValueDictionary(routeValues)));

            linkBuilder.InnerHtml = imageBuilder.ToString(TagRenderMode.SelfClosing) + " " + text;

            return linkBuilder.ToString(TagRenderMode.Normal);
        }

        public static string ImageLink(this HtmlHelper html, object routeValues, string imageURL, string text, string classes, string style, bool imageLeft)
        {
            // Create an instance of UrlHelper
            var url = new UrlHelper(html.ViewContext.RequestContext);
            // Create image tag builder
            var imageBuilder = new TagBuilder("img");
            // Add image attributes
            var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
            imageBuilder.MergeAttribute("src", urlHelper.Content(imageURL));
            imageBuilder.MergeAttribute("alt", text);
            // Create link tag builder
            var linkBuilder = new TagBuilder("a");
            // Add attributes
            linkBuilder.MergeAttribute("class", "imageLink " + classes);
            linkBuilder.MergeAttribute("href", "javascript:void(0);");
            linkBuilder.MergeAttribute("style", style);

            if (imageLeft)
                linkBuilder.InnerHtml = imageBuilder.ToString(TagRenderMode.SelfClosing) + " " + text;
            else
                linkBuilder.InnerHtml = text + " " + imageBuilder.ToString(TagRenderMode.SelfClosing);

            return linkBuilder.ToString(TagRenderMode.Normal);
        }
    }
}